package club.banyuan.hrms.dao.impl;

import club.banyuan.hrms.dao.DepartmentDao;
import club.banyuan.hrms.domain.Department;

import java.util.ArrayList;
import java.util.List;

public class DepartmentDaoImpl implements DepartmentDao {
    private static List<Department> departments;

    static {
        departments = new ArrayList<>();

        Department department = new Department();
        department.setId(1);
        department.setName("研发部");
        department.setDescription("无");
        departments.add(department);

        department = new Department();
        department.setId(2);
        department.setName("产品部");
        department.setDescription("无");
        departments.add(department);
    }

    @Override
    public List<Department> getDepartments() {
        return departments;
    }

    @Override
    public void addDepartment(Department department) {
        department.setId(departments.size() + 1);
        departments.add(department);
    }

    @Override
    public void updateDepartment(Department department) {
        Integer id = department.getId();
        if (id != null) {
            Department existDepartment = getDepartmentById(id);
            existDepartment.setDescription(department.getDescription());
            existDepartment.setName(department.getName());
        }
    }

    @Override
    public Department getDepartmentById(Integer id) {
        for (Department department : departments) {
            if (department.getId().equals(id)) {
                return department;
            }
        }
        return null;
    }

    @Override
    public void deleteDepartment(Integer[] ids) {
        for (Integer id : ids) {
            departments.remove(getDepartmentById(id));
        }
    }
}
